import {createRouter, createWebHashHistory} from 'vue-router'
// 引入login文件
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'
import WelCome from '../components/welcome.vue'
import UserList from '../components/admin/UserList.vue'
import ModifyPermission from '../components/admin/ModifyPermission.vue'
import sport from '../components/admin/sport.vue'
import goods from '../components/admin/goods.vue'
import Introduction from '../components/admin/Introduction.vue'
import calories from '../components/admin/calories.vue'
import food from '../components/admin/food.vue'

const routes = [
    {
        path: '/',
        // 记住此处是双引号
        redirect: "/login"
    },
    {
        // 记住此处是单引号，
        path: '/login',
        component: Login
    },
    {
        path:'/home',
        component: Home,
        redirect:'/welcome',
        children:[
            { path:'/welcome', component: WelCome, },
            { path:'/UserList', component: UserList, },
            { path:'/ModifyPermission', component: ModifyPermission, },
            { path:'/sport', component: sport, },
            { path:'/Introduction', component: Introduction, },
            { path:'/calories', component: calories, },
            { path:'/food', component: food, },
            { path:'/goods', component: goods, }


    ]
    },
]

const router = createRouter({
    history: createWebHashHistory(),
    routes
})
// 挂载路由导航卫士
router.beforeEach((to,from,next)=>{
    // to是将要访问的地址，
    // from表示从哪里来
    // next表示继续访问to的地址，next(url)表示重定向到什么地方，
    if(to.path=="/login") return next();//入果要访问首页表示放行
    // 否则看一下是否有登录来进行限制。
    const userFlag =window.sessionStorage.getItem("user");//取出当前用户
    if(!userFlag) return next("/login");//无值不能跳转，返回首页
    // 有值可以跳转
    console.log("放行了");
    next();//符合要求进行放行。

})
export default router
